package cn.itsource.smartsystem.service.impl;

import cn.itsource.smartsystem.domain.Student;
import cn.itsource.smartsystem.domain.Teacher;
import cn.itsource.smartsystem.mapper.StuMapper;
import cn.itsource.smartsystem.service.StuService;
import cn.itsource.smartsystem.util.ExcelUtil;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)//注意 在增删改上需要加transactional注解
public class StuServiceImpl implements StuService {
    @Autowired
    private StuMapper smapper;
    @Override
    public List<Student> getAll() {

        return smapper.getAll();
    }
    @Transactional
    @Override
    public Integer insertStudent(Student stu) {
        return smapper.insert(stu);
    }
    @Transactional
    @Override
    public Integer updateByPrimaryKeySelective(Student stu) {
        return smapper.updateByPrimaryKeySelective(stu);
    }
    @Transactional
    @Override
    public Integer deleteByPrimaryKey(Long id) {
        return smapper.deleteByPrimaryKey(id);
    }
    @Override
    public void importExcel(MultipartFile file) {
        try {
            // 1.使用工具类从复杂文件中获取到学生信息集合
            List<Student> students = ExcelUtil.importExcel(file, Student.class);
            // 2.调用批量新增方法，保存所有学生信息
            smapper.batchAdd(students);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
